#include <iostream>
#include <string>
#include <vector>

// 字符串的分割
size_t Split(const std::string &src, const std::string &sep, std::vector<std::string> *arry)
{
    size_t offset = 0;
    while (offset < src.size())
    {
        // 记录分隔符
        size_t pos = src.find(sep, offset);
        /**
         * 当没有找到分隔符时(也就是走到了字符串末尾)
         * "abc,def,tyu"
         * "abc,def,tyu,"-->对应的判断if(pos == src.size()) break;
         */
        if (pos == std::string::npos)
        {
            // 如果末尾是个sep,那么就不打印了
            if (pos == src.size())
                break;
            // 如果末尾不是sep,那就把末尾的子串添加到arry中
            arry->push_back(src.substr(offset));
            return arry->size();
        }
        /**
         * 当src中出现连续的sep,那么也不打印直接跳过
         * "abc,,,,def,tyu"-->if(pos == offset)
         */
        if (pos == offset)
        {
            offset = pos + sep.size();
            continue;
        }
        arry->push_back(src.substr(offset, pos - offset));
        offset = pos + sep.size();
    }
    return arry->size();
}

int main()
{
    std::string src = "abc,,,,def,gh,,,,";
    std::string sep = ",";
    std::vector<std::string> arry;
    Split(src, sep, &arry);
    for (auto &e : arry)
    {
        std::cout << "[" << e << "]" << std::endl;
    }

    return 0;
}